#include <QtMath>
#include "cg_jeuadversaire.h"

Cg_JeuAdversaire::Cg_JeuAdversaire() : m_numeroJoueur(0)
{

}

Cg_JeuAdversaire::Cg_JeuAdversaire(int numero) : m_numeroJeu(numero), m_numeroJoueur(0)
{

}

void Cg_JeuAdversaire::update()
{
    int i = 0, x = 0, y = 0;
    double angle = 0;
    switch (m_numeroJeu)
    {
        case 1 :
            x = 794, y = 1070;
            angle = -60+24*(18-m_cartes.size())/7;
            if (m_cartes.size()>0)
            {
                m_cartes[0]->setPlace(i);
                m_cartes[0]->setOffset(x,y-216);
                m_cartes[0]->setTransformOriginPoint(x,y);
                m_cartes[0]->setRotation(angle);
                m_cartes[0]->update();
                for(i=1;i<m_cartes.size();i++)
                {
                    m_cartes[i]->setPlace(i);
                    x += 23*qSin(qDegreesToRadians(angle-270));
                    y -= 23*qCos(qDegreesToRadians(angle-270));
                    m_cartes[i]->setOffset(x,y-216);
                    m_cartes[i]->setTransformOriginPoint(x,y);
                    angle += 6;
                    m_cartes[i]->setRotation(angle);
                    m_cartes[i]->update();
                }
            }
            break;

        case 2 :
            x = 1778,y = 733;
            angle = -137+24*(15-m_cartes.size())/7;
            if (m_cartes.size()>0)
            {
                m_cartes[0]->setPlace(i);
                m_cartes[0]->setOffset(x,y-160);
                m_cartes[0]->setTransformOriginPoint(x,y);
                m_cartes[0]->setRotation(angle);
                m_cartes[0]->update();
                for(i=1;i<m_cartes.size();i++)
                {
                    m_cartes[i]->setPlace(i);
                    x += 17*qSin(qDegreesToRadians(angle-270));
                    y -= 17*qCos(qDegreesToRadians(angle-270));
                    m_cartes[i]->setOffset(x,y-160);
                    m_cartes[i]->setTransformOriginPoint(x,y);
                    angle += 6;
                    m_cartes[i]->setRotation(angle);
                    m_cartes[i]->update();
                }
            }
            break;

        case 3 :
            x = 1486,y = 313;
            angle = 168+24*(15-m_cartes.size())/7;
            if (m_cartes.size()>0)
            {
                m_cartes[0]->setPlace(i);
                m_cartes[0]->setOffset(x,y-160);
                m_cartes[0]->setTransformOriginPoint(x,y);
                m_cartes[0]->setRotation(angle);
                m_cartes[0]->update();
                for(i=1;i<m_cartes.size();i++)
                {
                    m_cartes[i]->setPlace(i);
                    x += 17*qSin(qDegreesToRadians(angle-270));
                    y -= 17*qCos(qDegreesToRadians(angle-270));
                    m_cartes[i]->setOffset(x,y-160);
                    m_cartes[i]->setTransformOriginPoint(x,y);
                    angle += 6;
                    m_cartes[i]->setRotation(angle);
                    m_cartes[i]->update();
                }
            }
            break;

        case 4 :
            x = 706,y = 55;
            angle =92+24*(15-m_cartes.size())/7;
            if (m_cartes.size()>0)
            {
                m_cartes[0]->setPlace(i);
                m_cartes[0]->setOffset(x,y-160);
                m_cartes[0]->setTransformOriginPoint(x,y);
                m_cartes[0]->setRotation(angle);
                m_cartes[0]->update();
                for(i=1;i<m_cartes.size();i++)
                {
                    m_cartes[i]->setPlace(i);
                    x += 17*qSin(qDegreesToRadians(angle-270));
                    y -= 17*qCos(qDegreesToRadians(angle-270));
                    m_cartes[i]->setOffset(x,y-160);
                    m_cartes[i]->setTransformOriginPoint(x,y);
                    angle += 6;
                    m_cartes[i]->setRotation(angle);
                    m_cartes[i]->update();
                }
            }
            break;

        case 5 :
            x = 107,y = 541;
            angle = 31+24*(15-m_cartes.size())/7;
            if (m_cartes.size()>0)
            {
                m_cartes[0]->setPlace(i);
                m_cartes[0]->setOffset(x,y-160);
                m_cartes[0]->setTransformOriginPoint(x,y);
                m_cartes[0]->setRotation(angle);
                m_cartes[0]->update();
                for(i=1;i<m_cartes.size();i++)
                {
                    m_cartes[i]->setPlace(i);
                    x += 17*qSin(qDegreesToRadians(angle-270));
                    y -= 17*qCos(qDegreesToRadians(angle-270));
                    m_cartes[i]->setOffset(x,y-160);
                    m_cartes[i]->setTransformOriginPoint(x,y);
                    angle += 6;
                    m_cartes[i]->setRotation(angle);
                    m_cartes[i]->update();
                }
            }
            break;
    }
}

void Cg_JeuAdversaire::ajouterCarte(Cg_CarteAdverse *carte)
{
    m_cartes.push_back(carte);
    carte->setJeu(this);
}

void Cg_JeuAdversaire::ajouterCarte(Cg_CarteAdverse *carte, bool b)
{
    m_cartes.insert(carte->getPlace(),carte);
    if (b)
        carte->setJeu(this);
}

void Cg_JeuAdversaire::retirerCarte(Cg_CarteAdverse *carte)
{
    int i = 0;
    while ((i<m_cartes.size()) && (m_cartes[i] != carte))
    {
        i++;
    }
    if (m_cartes[i] == carte)
        m_cartes.remove(i);
}

void Cg_JeuAdversaire::effacerJeu()
{
    int i = 0;
    while (!(m_cartes.isEmpty()))
    {
        qDebug() << "effacerJeu - Cg_Cg_JeuJoueur - iteration " << i << "\n";
        QPixmap rien;
        m_cartes[0]->setPixmap(rien);
        m_cartes.pop_front();
        i++;
    }
}

void Cg_JeuAdversaire::numero_change(unsigned int n)
{
    m_numeroJoueur = n;
}

void Cg_JeuAdversaire::carte_jouee(unsigned int joueur, Carte carte)
{
    if ((joueur == (m_numeroJeu - m_numeroJoueur + 7) % 5) && (carte.numero() != 78))
    {

        QPixmap rien;
        m_cartes[0]->setPixmap(rien);
        m_cartes.pop_front();
    }
}
